import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'
import { UserService } from '../user/user.service'

@Injectable()
export class AdminGuard implements CanActivate {
  constructor(private userService: UserService) {}
  async canActivate(context: ExecutionContext): Promise<boolean> {
    const request = context.switchToHttp().getRequest()
    const user = request.user
    const { roles = [] } = await this.userService.findOne(user.userId)
    if (roles.filter((o) => o.id === 1).length > 0) {
      return true
    }
    return false
  }
}
